import os

import requests
from flask import Blueprint, request, Response, stream_with_context, g, current_app
from werkzeug.utils import secure_filename

from controller.public import get_enum_list, get_all_permission
from models.user import Permission, UserRolePermission
from utils.upload import Upload
from utils.views import resp_with_data
from components.base_exception import ParamsError, NotFoundError
from components.base_log import log_method_call

public_bp = Blueprint('public', __name__)


@public_bp.route('/get_enum', methods=['GET'])
@log_method_call(action='')
def get_enum():
    data = get_enum_list()
    return resp_with_data(10000, data)


@public_bp.route('/upload', methods=['POST'])
@log_method_call(action='')
def upload_file():
    if not (g.files and 'file' in g.files and g.files['file']):
        return resp_with_data(10012)
    file = g.files['file']
    filename = Upload().upload_one(file)
    data = {
        "url": "/static/upload/" + filename
    }
    return resp_with_data(10000, data)


@public_bp.route('/permissions', methods=['GET'])
@log_method_call(action='')
def permissions():
    """
    给所有的权限
    """
    data = get_all_permission()
    return resp_with_data(10000, data)
